package report

import (
	"../common"
	"../web"
	"encoding/json"
	"strconv"
	"strings"
)

type QueryWoWipObj struct {
	common.BaseHandler
}

type QueryWoWipDetailObj struct {
	common.BaseHandler
}

type QueryWoWipPageObj struct {
	common.BaseHandler
}

func (self *QueryWoWipObj) Prepare() {
	stations := *common.FetchAllRowsPtr(
		`SELECT STATION, ''
      	   FROM SFC_STATION(NOLOCK) 
    	  ORDER BY STATION`,
		self.Usr.Conn, new(common.Station))

	self.TmplData["stations"] = stations
}

func (self *QueryWoWipObj) ParameterCheck() bool {
	s_wono := strings.TrimSpace(self.Ctx.Params["wono"])
	s_stat := strings.TrimSpace(self.Ctx.Params["station"])
	if s_wono == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_wono)
	self.Sqlparas = append(self.Sqlparas, s_stat)

	return true
}

func (self *QueryWoWipObj) SqlQueryExecute() {
	var wip_ls []interface{}
	if self.Sqlparas[1].(string) == "" {
		wip_ls = *common.FetchAllRowsPtr(
			`SELECT WONO, CURR_STATION, COUNT(SSN_NO) AS WIP 
			   FROM SFC_SSN(NOLOCK) 
			  WHERE WONO=? 
			    AND CURR_STATION <> 'SFCF' 
			  GROUP BY WONO, CURR_STATION`,
			self.Usr.Conn, new(common.WipQuery), self.Sqlparas[0].(string))
	} else {
		wip_ls = *common.FetchAllRowsPtr(
			`SELECT WONO, CURR_STATION, COUNT(SSN_NO) AS WIP 
			   FROM SFC_SSN(NOLOCK) 
			  WHERE WONO=? 
				AND CURR_STATION=? 
			  GROUP BY WONO, CURR_STATION`,
			self.Usr.Conn, new(common.WipQuery), self.Sqlparas...)
	}

	self.TmplData["wip_ls"] = wip_ls
}

func QueryWOWIPHandler(ctx *web.Context) {
	self := new(QueryWoWipObj)
	self.Ctx = ctx
	self.Html = "rprwip.html"
	self.Rightlist = []string{"rprwip"}
	self.DoQuery(self)
}

func (self *QueryWoWipDetailObj) ParameterCheck() bool {
	s_wono := self.Ctx.Params["workorder"]
	s_stat := self.Ctx.Params["station"]

	if s_wono == "" || s_stat == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_wono)
	self.Sqlparas = append(self.Sqlparas, s_stat)
	self.TmplData["wono"] = s_wono
	self.TmplData["station"] = s_stat

	return true
}

func (self *QueryWoWipDetailObj) SqlQueryExecute() {
	ssn_main := *common.FetchAllRowsPtr(
		`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO), A.WONO, B.ITEM, 
                 A.SSN_NO, A.CURR_STATION, A.RESULT, ISNULL(A.FAILCODE, '')
           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK) 
          WHERE A.WONO=? 
            AND A.CURR_STATION=? 
            AND A.WONO=B.WONO`,
		self.Usr.Conn, new(common.WipDetail), self.Sqlparas...)

	curr_id := 1
	iRtnData := *common.FetchOneRowPtr(
		`SELECT COUNT(SSN_NO)
           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK) 
          WHERE A.WONO=? 
            AND A.CURR_STATION=? 
            AND A.WONO=B.WONO`,
		self.Usr.Conn, new(common.RecCount), self.Sqlparas...)
	recnum := iRtnData.(common.RecCount).Number
	total_id := recnum / 10
	if recnum%10 > 0 {
		total_id = total_id + 1
	}

	self.TmplData["ssn_main"] = ssn_main
	self.TmplData["curr_page"] = curr_id
	self.TmplData["total_page"] = total_id
}

func QueryWOWIPDetailHandler(ctx *web.Context) {
	self := new(QueryWoWipDetailObj)
	self.Ctx = ctx
	self.Html = "wowipdetail.html"
	self.Rightlist = []string{"rprwip"}
	self.DoQueryUrl(self)
}

func (self *QueryWoWipPageObj) ParameterCheck() bool {
	s_wono := self.Ctx.Params["wono"]
	s_page := self.Ctx.Params["pageid"]
	s_stat := self.Ctx.Params["station"]
	if s_wono == "" {
		return false
	}
	pageid, err := strconv.Atoi(s_page)
	if err != nil {
		return false
	}
	if s_stat == "" {
		return false
	}
	idx := 10 * (pageid - 1)

	self.Sqlparas = append(self.Sqlparas, s_wono)
	self.Sqlparas = append(self.Sqlparas, s_stat)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *QueryWoWipPageObj) SqlQueryExecute() {
	wonos := *common.FetchAllRowsPtr(
		`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, A.SSN_NO, A.STATION, A.RESULT, A.FCOD
           FROM
                (
                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, 
                			B.ITEM, A.SSN_NO, A.CURR_STATION AS STATION,
                    	    A.RESULT, ISNULL(A.FAILCODE, '') AS FCOD
                  	  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK)
                 	 WHERE A.WONO=? 
                   	   AND A.CURR_STATION=? 
                   	   AND A.WONO=B.WONO
              	) A
          WHERE A.AUTOID > ?`,
		self.Usr.Conn, new(common.WipDetail), self.Sqlparas...)

	dict_js, _ := json.Marshal(map[string]interface{}{"wonos": wonos, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitWoWipByPageHandler(ctx *web.Context) {
	self := new(QueryWoWipPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}
